package com.reader.cms.service.impl.sys;

import com.baomidou.mybatisplus.plugins.Page;
import com.reader.cms.entity.sys.SysMenu;
import com.reader.cms.entity.sys.SysUser;
import com.reader.cms.mapper.sys.SysMenuMapper;
import com.reader.cms.service.sys.ISysMenuService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jiahaikun
 * @date 2018-07-26
 */
@Service("iSysMenuService")
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements ISysMenuService {
    @Autowired
    private SysMenuMapper sysMenuMapper;

    /**
     * 递归获取子菜单
     * @param menuList  菜单集合
     * @param menu 父菜单
     */
    private void getMenuChild(List<SysMenu> menuList,SysMenu menu){
        List<SysMenu>  menuChildren=menuList.stream().filter(a -> a.getParentId().equals(menu.getId())).collect(Collectors.toList());
        menu.setChildren(menuChildren);
        for(SysMenu  subMenu :menuChildren){
            getMenuChild(menuList,subMenu); //每次要传条件查出的所有菜单集合
        }
    }

    /**
     * 查询用户有权限的菜单
     * @param sysUser
     * @return
     */
    @Override
    public List<SysMenu> getUserMenus(SysUser sysUser) {
        List<SysMenu> meunList = sysMenuMapper.getUserMenus(sysUser);
        System.out.print(meunList.size());
        //最项层菜单 level 0
        List<SysMenu> level0Menus = meunList.stream().filter(a -> a.getParentId()==0).collect(Collectors.toList());
        for (SysMenu menu : level0Menus){
            getMenuChild(meunList,menu);
        }
        return level0Menus;
    }

    /**
     * 查询菜单,左侧菜单栏显示
     * @return
     */
    @Override
    public List<SysMenu> getAllMenus() {
        List<SysMenu> meunList = sysMenuMapper.getAllMenus();
        System.out.print(meunList.size());
        //最项层菜单 level 0
        List<SysMenu> level0Menus = meunList.stream().filter(a -> a.getParentId()==0).collect(Collectors.toList());
        for (SysMenu menu : level0Menus){
            getMenuChild(meunList,menu);
        }
        return level0Menus;
    }


    /**
     * 查询所有菜单，管理设置用
     * @return
     */

    @Override
    public List<SysMenu> getAllMenusManagement() {
        List<SysMenu>  meunList= sysMenuMapper.getAllMenusManagement();
        List<SysMenu> level0Menus = meunList.stream().filter(a -> a.getParentId()==0).collect(Collectors.toList());
        for (SysMenu menu : level0Menus){
            getMenuChild2(meunList,menu);
        }
        return level0Menus;
    }

    /**
     * 递归获取子菜单,管理设置用
     * @param menuList  菜单集合
     * @param menu 父菜单
     */
    private void getMenuChild2(List<SysMenu> menuList,SysMenu menu){
        List<SysMenu>  menuChildren=menuList.stream().filter(a -> a.getParentId().equals(menu.getId())).collect(Collectors.toList());
        if(menuChildren.size() != 0){ menu.setChildren(menuChildren);
            for(SysMenu  subMenu :menuChildren){
                getMenuChild2(menuList,subMenu); //每次要传条件查出的所有菜单集合
            }
        }
    }
    @Override
    public Page<Map<String, Object>> selectListPage(int currentPage, int size, Integer parentId, String name) {
        Page<Map<String, Object>> page = new Page<>(currentPage, size);// 当前页，总条数 构造 page 对象
        return page.setRecords(sysMenuMapper.selectListPage(page,parentId,name));
    }

    @Override
    public List<SysMenu> getAllResources() {
        List<SysMenu> meunList = sysMenuMapper.getAllResources();
        //最项层菜单 level 0
        List<SysMenu> level0Menus = meunList.stream().filter(a -> a.getParentId()==0).collect(Collectors.toList());
        for (SysMenu menu : level0Menus){
            getMenuChild(meunList,menu);
        }
        return level0Menus;
    }

    @Override
    public List<Integer> getRoleMenu(Integer id) {

        return sysMenuMapper.getRoleMenu(id);
    }


}
